#!/usr/bin/env bash
# todisc-fade-routine
# Part of the tovid suite
# =======================
# A bash script that dynamically creates the fade algorithm
# needed by todisc for menu fades
#
# Project homepage: http://tovid.wikia.com
#
# Copyright (C) 2005-2010
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Or see:
#
#     http://www.gnu.org/licenses/gpl.txt


# Mostly written by  Joe "MacNorth" Friedrichsen:


# user-defined variables....
FRAME_RATE=${FRAME_RATE:-29.97}
OPACITY=${OPACITY:-100}
MENU_LEN=${MENU_LEN:-20}
DISSOLVE_LIMIT=$OPACITY

# get things in order
# ##########################
# Define variable limits....
FADE_FLOOR=0
FADE_CEILING=100
MIST_CEILING=33

# ##############
# New variables!
# fade times (in seconds)
BG_FADE_LEN=0.7
TITLE_FADE_LEN=1.0
THUMBS_FADE_LEN=1.0

# ##############
# many of the following vars can be defined by the calling (sourcing) script
# look for ${FOO:-BAR} syntax or vars in small caps *and* LARGE CAPS

# fade in points (in seconds)
BG_FADEIN_STARTTIME=0
BG_FADEIN_ENDTIME=$(bc_math "$BG_FADEIN_STARTTIME + $BG_FADE_LEN")

TITLE_FADEIN_STARTTIME=${TITLE_FADEIN_STARTTIME:-1}
TITLE_FADEIN_ENDTIME=$(bc_math "$TITLE_FADEIN_STARTTIME + $TITLE_FADE_LEN")

THUMBS_FADEIN_STARTTIME=${THUMBS_FADEIN_STARTTIME:-3.5}
THUMBS_FADEIN_ENDTIME=$(bc_math "$THUMBS_FADEIN_STARTTIME + $THUMBS_FADE_LEN")

# fade out points (in seconds)
BG_FADEOUT_ENDTIME=$MENU_LEN

bg_fadeout_start_time=$(bc_math "$BG_FADEOUT_ENDTIME - $BG_FADE_LEN")
BG_FADEOUT_STARTTIME=${BG_FADEOUT_STARTTIME:-$bg_fadeout_start_time}


title_fadeout_end_time=$(bc_math "$MENU_LEN - $BG_FADE_LEN")
TITLE_FADEOUT_ENDTIME=${TITLE_FADEOUT_ENDTIME:-$title_fadeout_end_time}

title_fadeout_start_time=$(bc_math "$TITLE_FADEOUT_ENDTIME - $TITLE_FADE_LEN")
TITLE_FADEOUT_STARTTIME=${TITLE_FADEOUT_STARTTIME:-$title_fadeout_start_time}


thumbs_fadeout_end_time=$(bc_math "$BG_FADEOUT_STARTTIME - $THUMBS_FADE_LEN")
THUMBS_FADEOUT_ENDTIME=${THUMBS_FADEOUT_ENDTIME:-$thumbs_fadeout_end_time}

thumbs_fadeout_start_time=$(bc_math \
 "$THUMBS_FADEOUT_ENDTIME - $THUMBS_FADE_LEN")
THUMBS_FADEOUT_STARTTIME=${THUMBS_FADEOUT_STARTTIME:-$thumbs_fadeout_start_time}


# ################################################
# Translate things for frame-by-frame construction
ANIMENU_ENDFRAME=$(bc_math "$FRAME_RATE * $MENU_LEN" int)

BG_FADEIN_STARTFRAME=$(bc_math "$FRAME_RATE * $BG_FADEIN_STARTTIME" int)

BG_FADEIN_ENDFRAME=$(bc_math "$FRAME_RATE * $BG_FADEIN_ENDTIME" int)

TITLE_FADEIN_STARTFRAME=$(bc_math "$FRAME_RATE * $TITLE_FADEIN_STARTTIME" int)

TITLE_FADEIN_ENDFRAME=$(bc_math "$FRAME_RATE * $TITLE_FADEIN_ENDTIME" int)

THUMBS_FADEIN_STARTFRAME=$(bc_math "$FRAME_RATE * $THUMBS_FADEIN_STARTTIME" int)

THUMBS_FADEIN_ENDFRAME=$(bc_math "$FRAME_RATE * $THUMBS_FADEIN_ENDTIME" int)

BG_FADEOUT_STARTFRAME=$(bc_math "$FRAME_RATE * $BG_FADEOUT_STARTTIME" int)

BG_FADEOUT_ENDFRAME=$(bc_math "$FRAME_RATE * $BG_FADEOUT_ENDTIME" int)

TITLE_FADEOUT_STARTFRAME=$(bc_math "$FRAME_RATE * $TITLE_FADEOUT_STARTTIME" int)

TITLE_FADEOUT_ENDFRAME=$(bc_math "$FRAME_RATE * $TITLE_FADEOUT_ENDTIME" int)

THUMBS_FADEOUT_STARTFRAME=$(bc_math "$FRAME_RATE \
 * $THUMBS_FADEOUT_STARTTIME" int)

THUMBS_FADEOUT_ENDFRAME=$(bc_math "$FRAME_RATE * $THUMBS_FADEOUT_ENDTIME" int)

# Set fade rates (rise over run or alpha over frame number)
BG_FADE_RATE=$(bc_math "($FADE_CEILING - $FADE_FLOOR) \
/ ($FRAME_RATE * $BG_FADE_LEN)")
TITLE_FADE_RATE=$(bc_math "($FADE_CEILING - $FADE_FLOOR) \
/ ($FRAME_RATE * $TITLE_FADE_LEN)")
MIST_FADE_RATE=$(bc_math "($MIST_CEILING - $FADE_FLOOR) \
/ ($FRAME_RATE * $TITLE_FADE_LEN)")
THUMBS_FADE_RATE=$(bc_math "($DISSOLVE_LIMIT - $FADE_FLOOR) \
/ ($FRAME_RATE * $THUMBS_FADE_LEN)")

get_bg_opacity ()
{
    ((0 <= frame && frame < BG_FADEIN_STARTFRAME)) && D=$FADE_FLOOR
    if ((BG_FADEIN_STARTFRAME <=frame && frame < BG_FADEIN_ENDFRAME)); then
        D=$(bc_math "($FADE_FLOOR \
        + ( ($frame + $BG_FADEIN_STARTFRAME) * $BG_FADE_RATE))")
    fi
    if ((BG_FADEIN_ENDFRAME <=frame && frame < BG_FADEOUT_STARTFRAME)); then
        D=$FADE_CEILING
    fi
    if ((BG_FADEOUT_STARTFRAME <=frame && frame < BG_FADEOUT_ENDFRAME)); then
        D=$(bc_math "($FADE_CEILING - ( ($frame - $BG_FADEOUT_STARTFRAME) \
        * $BG_FADE_RATE ))" )
    fi

    if ((BG_FADEOUT_ENDFRAME <=frame && frame <= ANIMENU_ENDFRAME)); then
        D=$FADE_FLOOR
    fi
  echo $D
}

get_title_opacity ()
{
    if ((0 <=frame && frame < TITLE_FADEIN_STARTFRAME)); then
        B=$FADE_FLOOR; C=$FADE_FLOOR
    fi
    if ((TITLE_FADEIN_STARTFRAME<=frame && frame<TITLE_FADEIN_ENDFRAME)); then
        B=$(bc_math "(($frame - $TITLE_FADEIN_STARTFRAME) * $MIST_FADE_RATE)")
        C=$(bc_math "(($frame - $TITLE_FADEIN_STARTFRAME) * $TITLE_FADE_RATE)")
    fi
    if ((TITLE_FADEIN_ENDFRAME<=frame && frame<TITLE_FADEOUT_STARTFRAME)); then
        B=$MIST_CEILING; C=$FADE_CEILING
    fi
    if ((TITLE_FADEOUT_STARTFRAME<=frame && frame<TITLE_FADEOUT_ENDFRAME)); then
        B=$(bc_math "($MIST_CEILING - (($frame - $TITLE_FADEOUT_STARTFRAME) \
        * $MIST_FADE_RATE))" )
        C=$(bc_math "($FADE_CEILING - ( ($frame - $TITLE_FADEOUT_STARTFRAME) \
        * $TITLE_FADE_RATE))" )
    fi
    if ((TITLE_FADEOUT_ENDFRAME <=frame && frame <=ANIMENU_ENDFRAME)); then
        B=$FADE_FLOOR; C=$FADE_FLOOR
    fi

    echo $B:$C
}

get_thumb_opacity ()
{
    ((0 <=frame && frame < THUMBS_FADEIN_STARTFRAME)) && S=$FADE_FLOOR
    if ((THUMBS_FADEIN_STARTFRAME<=frame && frame<THUMBS_FADEIN_ENDFRAME)); then
        S=$(bc_math "($FADE_FLOOR + (($frame - $THUMBS_FADEIN_STARTFRAME) \
         * $THUMBS_FADE_RATE))" )
    fi
    if ((THUMBS_FADEIN_ENDFRAME<=frame \
     && frame<THUMBS_FADEOUT_STARTFRAME)); then
        S=$DISSOLVE_LIMIT
    fi
    if ((THUMBS_FADEOUT_STARTFRAME <=frame \
     && frame < THUMBS_FADEOUT_ENDFRAME)); then
        S=$(bc_math "($DISSOLVE_LIMIT \
         - (($frame - $THUMBS_FADEOUT_STARTFRAME) * $THUMBS_FADE_RATE))" ) 
    fi
    if (( THUMBS_FADEOUT_ENDFRAME <=frame && frame <=ANIMENU_ENDFRAME)) ; then
        S=$FADE_FLOOR
    fi

    echo $S
}

# Initialize a 'random' array of gamma values. There are 5 input variables:
#   $1 = the average gamma value
#   $2 = the magnitude of the main gamma variation (0.1 is a good start)
#   $3 = the time before the main variation repeats itself (in seconds)
#   $4 = the magnitude of the minor gamma variation (half of main is good)
#   $5 = the time before the minor variation repeats itself (in seconds)
#
#   OUTPUT = a newline separated list of numbers
#
# USAGE:
#           init_gamma_array average main_mag main_time minor_mag minor_time
# EXAMPLE:
#           GAMMA_VALUES="`init_gamma_array 1 0.1 5 0.05 0.1 | tr '\n' ' '`"
#           GAMMA_ARRAY=( "$GAMMA_VALUES" )

init_gamma_array ()
{
bc -l <<BC_EOF
pi=4*a(1);

start_frame=0

# Get values from standard in
# Format:
# echo "29.97\n 300\n 10\n .1\n 5\n .05\n .1\n" | bc -l
frame_rate=$FRAME_RATE;
end_frame=$ANIMENU_ENDFRAME;
average=$1;
amplitude=$2;
period=$3;
am_amplitude=$4;
am_period=$5;

scale=3;
for (i=start_frame; i <= end_frame; i++) {
  current_frame=i;
  x=current_frame / frame_rate;

  # AM modulation
  gamma=average + (amplitude + am_amplitude*s(x*2*pi/am_period)) \
  * s(x*2*pi/period);

  print gamma, "\n";
}
quit
BC_EOF
}

# example usage
#while ((frame <= $ANIMENU_ENDFRAME)); do
#    BG=$(get_bg_opacity)
#    MIST_TEXT=$(get_title_opacity)
#    MIST=$(awk -F ':' '{print $1'}<<<$MIST_TEXT)
#    TEXT=$(awk -F ':' '{print $2'}<<<$MIST_TEXT)
#    MONTAGE=$(get_thumb_opacity)
#    time=$(echo "scale=2; $frame / $FRAME_RATE" | bc)s
#    echo "$frame ($time) | BG: $BG mist: $MIST text: $TEXT montage: $MONTAGE"
#    ((frame++))
#done
GAMMA_VALUES="`init_gamma_array 1 0.01 5 0.01 1 | tr '\n' ' '`"
GAMMA_ARRAY=( "$GAMMA_VALUES" )
CURVE_VARS=${CURVE_VARS:-"20 10 5 10 1"}
CURVE_VALUES="`init_gamma_array  $CURVE_VARS | tr '\n' ' '`"
CURVE_ARRAY=( $CURVE_VALUES )

